//
//  ZZInputView.m
//  ZZInputView_Example
//
//  Created by 郑程旭 on 2022/2/19.
//  Copyright © 2022 qq282424020. All rights reserved.
//

#import "ZZInputView.h"
#import "ZZInputViewController.h"
#import "ZZInputMainView.h"

@interface ZZInputView () <ZZInputMainViewDelegate>

@property (nonatomic, strong) ZZInputViewController *inputVC;

@property (nonatomic, strong) ZZInputMainView *mainView;

@property (nonatomic, assign) CGFloat mainHeight;


@end

@implementation ZZInputView

- (instancetype)init {
    self = [super init];
    if (self) {
        
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor grayColor];
        self.mainHeight = 56;
        
        [self addKeyboardNotification];
        [self configViewController];
        [self configSubviews];
        
        [self.mainView.textView becomeFirstResponder];
    }
    return self;
}

#pragma mark - 控制器配置
- (void)configViewController {
    _inputVC = [[ZZInputViewController alloc] init];
    _inputVC.view = self;
    _delegate = _inputVC;
    _dataSource = _inputVC;
}

#pragma mark - Subviews
- (void)configSubviews {
    [self addSubview:self.mainView];
}

- (ZZInputMainView *)mainView {
    if (!_mainView) {
        _mainView = [[ZZInputMainView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.frame), CGRectGetWidth(self.frame), 56)];
        _mainView.delegate = self;
//        _mainView = [[ZZInputMainView alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.frame), 56)];
    }
    return _mainView;
}

#pragma mark - 键盘通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)addKeyboardNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)aNotification {
        
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration = [animationDurationValue doubleValue];
    CGFloat height = keyboardRect.size.height;
        
    [UIView animateWithDuration:animationDuration animations:^{
//        if (self.image && self.imageHeight == 0) {
//            self.imageHeight = 65;
//            self.mainHeight = self.mainHeight+self.imageHeight;
//            CGRect whiteFrame = self.textWhiteBgView.frame;
//            whiteFrame.size.height = whiteFrame.size.height+self.imageHeight;
//            self.textWhiteBgView.frame = whiteFrame;
//        }

        self.mainView.frame = CGRectMake(0, CGRectGetHeight(self.frame) - self.mainHeight - height, CGRectGetWidth(self.frame), self.mainHeight);

    }];
}

- (void)keyboardWillHide:(NSNotification *)aNotification {
    
    NSDictionary *userInfo = [aNotification userInfo];
    NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration = [animationDurationValue doubleValue];
    
    [UIView animateWithDuration:animationDuration animations:^{
        self.alpha = 0;
        self.mainView.frame = CGRectMake(0, CGRectGetHeight(self.frame), CGRectGetWidth(self.frame), self.mainHeight);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}


#pragma mark - ZZInputMainViewDelegate
//高度改变
- (void)inputMainViewHeightChanged:(CGFloat)height {
    self.mainHeight = height;
}

//选择相册
- (void)inputMainViewSelectPhotoLibrary {
    if ([_delegate respondsToSelector:@selector(inputViewSelectPhotoLibrary)]) {
        [_delegate inputViewSelectPhotoLibrary];
    }
}

//选择相机
- (void)inputMainViewSelectTakePhoto {
    if ([_delegate respondsToSelector:@selector(inputViewSelectTakePhoto)]) {
        [_delegate inputViewSelectTakePhoto];
    }
}

//选择录制语音
- (void)inputMainViewSelectRecordVoice {
    if ([_delegate respondsToSelector:@selector(inputViewSelectRecordVoice)]) {
        [_delegate inputViewSelectRecordVoice];
    }
}

#pragma mark - ReloadData
- (void)reloadData {
    if ([_dataSource respondsToSelector:@selector(inputViewFinishedSelectedPhoto)]) {
        UIImage *image = [_dataSource inputViewFinishedSelectedPhoto];
        NSLog(@".....%@",image);
    }
}

@end
